﻿using System;
using System.Collections.Generic;

using System.Text;
using Portal.Interface;
using Portal.Environment;
using Portal.Language;
using System.Data;
using System.Data.SqlClient;

namespace Portal.CMS
{
    public class LawDocumentSigner : WebPart
    {
        public override string WebPartId
        {
            get
            {
                return "LawDocumentSigner";
            }
        }

        public override bool IsMainWebPart
        {
            get
            {
                return true;
            }
        }

        public override string Title
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Title","Người ký (văn bản pháp quy)");
            }
        }

        public override string Description
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Description","");
            }
        }


        public override WebPartInt[] SubWebParts
        {
            get
            {
                WebPartInt[] SubWebParts = new WebPartInt[] 
                { 
                    new AddLawDocumentSigner(),new UpdateLawDocumentSigner()
                };
                return SubWebParts;
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetHtml(string Keyword, int CurrentPageIndex)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                Environments.CheckWebPartAllow(WebPartId, true);


                PermissionTypeCls PermissionTypeCls = FindPermissionType("LawDocumentSigner");
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "ListLawDocumentSigner", WebPartId, null, true);
             

                DataTable dtLawDocumentSigner = new LawDocumentSignerBLL().ReadingLawDocumentSigner(Keyword);
            
                int ItemPerPages = 15;
                int MaxPage = dtLawDocumentSigner.Rows.Count / ItemPerPages;
                if (dtLawDocumentSigner.Rows.Count % ItemPerPages != 0) MaxPage++;
                int StartIndex = CurrentPageIndex * ItemPerPages;
                int EndIndex = StartIndex + ItemPerPages;
                if (EndIndex > dtLawDocumentSigner.Rows.Count)
                {
                    EndIndex = dtLawDocumentSigner.Rows.Count;
                }


                string SelectPageText = "";
                if (MaxPage > 1)
                {
                    SelectPageText = ResourceBLL.GetText(WebPartId,"SelectPage","Trang") + ": <select id=divSelectPageLawDocumentSigner onchange=javascript:JumPageLawDocumentSigner();>";
                    for (int iIndex = 0; iIndex < MaxPage; iIndex++)
                    {
                        if (CurrentPageIndex == iIndex)
                        {
                            SelectPageText += "<option selected >" + (iIndex + 1) + "</option>";
                        }
                        else
                        {
                            SelectPageText += "<option>" + (iIndex + 1) + "</option>";
                        }
                    }
                    SelectPageText += "</select>";
                }

                string RetHtml =
                    ResourceBLL.GetText(WebPartId, "Keyword", "Từ khóa") + ": <input id=txtKeyword value=\"" + Keyword + "\">" + SelectPageText + "<input type=button value=\"" + ResourceBLL.GetText(WebPartId, "FilterButton", "Lọc") + "\" onclick=JumPageLawDocumentSigner();><input onclick=\"javascript:ClientAddLawDocumentSigner();\" type=button value=\"" + ResourceBLL.GetText(WebPartId, "TH_Add", "Thêm mới") + "\"><div id=divProcessing></div>";
                if (dtLawDocumentSigner.Rows.Count == 0)
                {
                    RetHtml += "<div class=\"cssNoData\">" + ResourceBLL.GetText(WebPartId, "NoData", "Chưa có dữ liệu nào trong chuyên mục này!") + "</div>";
                }
                else
                {
                    RetHtml +=
                    "   <div class=cssTotalResultInfo>" + ResourceBLL.GetText(WebPartId, "Found", "Số bản ghi thỏa mãn điều kiện tìm") + ": " + dtLawDocumentSigner.Rows.Count.ToString("#,##0") + "</div>" +
                    "   <table>" +
                    "       <tr>" +
                    "           <th>#</th>" +
                    "           <th>" + ResourceBLL.GetText(WebPartId, "TH_LawDocumentSignerName", "người ký") + "</th>" +
                    "           <th></th>" +
                    "           <th></th>" +
                    "       </tr>";

                    for (int iIndex = StartIndex; iIndex < EndIndex; iIndex++)
                    {
                        string LawDocumentSignerId = (string)dtLawDocumentSigner.Rows[iIndex]["LawDocumentSignerId"];
                        string LawDocumentSignerName = (string)dtLawDocumentSigner.Rows[iIndex]["Subject"];
                        string DeleteUrl = "javascript:ClientDeleteLawDocumentSigner('" + LawDocumentSignerId + "')";

                        RetHtml +=
                        "       <tr onMouseOver=\"this.className='highlight'\" onMouseOut=\"this.className='normal'\">" +
                        "           <td class=cssTdIndex>" + (iIndex + 1) + "</td>" +
                        "           <td  class=cssTdLawDocumentSignerName>" + LawDocumentSignerName + "</td>" +
                        "           <td><a href=\"javascript:ShowClientUpdateUrlForm('" + LawDocumentSignerId + "');\">" + ResourceBLL.GetText(WebPartId, "TH_Update", "Cập nhật") + "</a></td>" +
                        "           <td><a href=\"" + DeleteUrl + "\">" + ResourceBLL.GetText(WebPartId, "TH_Delete", "Xóa") + "</a></td>" +
                        "       </tr>";
                    }
                    RetHtml +=
                        "   </table>";
                }
                return RetHtml;
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }
        }

        public override string RenderWebPart(LoadParamCls loadParamCls)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(LawDocumentSigner));
            string RetHtml =
                WebPartUtilities.AttachJavascript("Portal.CMS", "LawDocumentSigner.js") +
                    "<div id=divLawDocumentSignerMain class=cssDivLawDocumentSigner>" + GetHtml("",0) + "</div>";
            return RetHtml;
        
        }



        [AjaxPro.AjaxMethod]
        public string GetAddGroupNewUrl()
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                PermissionTypeCls PermissionTypeCls = FindPermissionType("LawDocumentSigner");
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "AddLawDocumentSigner", WebPartId, null, true);

                return Screen.BuildDialogUrl(new ParamCls[]{
                new ParamCls("WebPartId",new AddLawDocumentSigner().WebPartId)
            });
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string GetConfirmDeleteMsg()
        {
            return ResourceBLL.GetText(WebPartId,"ConfirmDeleteMsg","Bạn đã chắc chắn xóa chưa?");
        }


        [AjaxPro.AjaxMethod]
        public string ServerSideCheckDeleteLawDocumentSigner(string LawDocumentSignerId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                PermissionTypeCls PermissionTypeCls = FindPermissionType("LawDocumentSigner");
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "DeleteLawDocumentSigner", WebPartId, null, true);
                return "";
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideDeleteLawDocumentSigner(string LawDocumentSignerId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                PermissionTypeCls PermissionTypeCls = FindPermissionType("LawDocumentSigner");
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "DeleteLawDocumentSigner", WebPartId, null, true);
           

                new LawDocumentSignerBLL().DeleteLawDocumentSigner(null, LawDocumentSignerId);
                return ResourceBLL.GetText(WebPartId,"DeleteOkMessage","Xóa thành công");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetShowUpdateForm(string LawDocumentSignerId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                PermissionTypeCls PermissionTypeCls = FindPermissionType("LawDocumentSigner");
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "UpdateLawDocumentSigner", WebPartId, null, true);

             
                string UpdateUrl = Screen.BuildDialogUrl(new ParamCls[]{
                        new ParamCls("WebPartId",new UpdateLawDocumentSigner().WebPartId),
                        new ParamCls("LawDocumentSignerId",LawDocumentSignerId)
                    });
                return UpdateUrl;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetProcessingText()
        {
            string Url = Environment.Environments.GetActiveThemeUrl() + "/Global/Loading.gif";
            return "<img src=\"" + Url + "\"><a class=cssProcessing>" + ResourceBLL.GetText(WebPartId,"ProcessingText","Đang đọc dữ liệu...") + "</a>";
        }

        public override bool SystemWebPart
        {
            get
            {
                return false;
            }
        }


        public override FunctionCls[] FunctionList
        {
            get
            {
                return new FunctionCls[]
                {
                    new FunctionCls("ListLawDocumentSigner",ResourceBLL.GetText(WebPartId,"ListLawDocumentSigner","Hiện danh sách người ký"),WebPartId),
                    new FunctionCls("AddLawDocumentSigner",ResourceBLL.GetText(WebPartId,"AddLawDocumentSigner","Thêm mới người ký"),WebPartId),
                    new FunctionCls("UpdateLawDocumentSigner",ResourceBLL.GetText(WebPartId,"UpdateLawDocumentSigner","Sửa chữa người ký"),WebPartId),
                    new FunctionCls("DeleteLawDocumentSigner",ResourceBLL.GetText(WebPartId,"DeleteLawDocumentSigner","Xóa người ký"),WebPartId),
                };
            }
        }

        public override PermissionTypeCls[] PermissionTypes
        {
            get
            {
                return new PermissionTypeCls[]{
                    new PermissionTypeCls("LawDocumentSigner","người ký",new string[]{"ListLawDocumentSigner","AddLawDocumentSigner","UpdateLawDocumentSigner","DeleteLawDocumentSigner"})
                };
            }
        }

        public override PermissionTypeCls GetServicePermissionType()
        {
            return PermissionTypes[0];
        }
    }
}
